package cn.bocweb.spinning.view.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.ErrorBean;
import cn.bocweb.spinning.model.Bean.GetMsgBean;
import cn.bocweb.spinning.model.Bean.UserBean;
import cn.bocweb.spinning.presenter.Imp.UserPresenterImp;
import cn.bocweb.spinning.presenter.UserPresenter;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.view.activity.MyMsgActivity;
import cn.bocweb.spinning.view.adapter.SystemMsgAdapter;
import cn.bocweb.spinning.view.common.SwipeRefreshHelper;
import cn.bocweb.spinning.view.dialog.LoadingDialog;
import de.greenrobot.event.EventBus;

/**
 * Created by chenchangxing on 15/12/21.
 */
public class SystemMsgFragment extends Fragment {
    @Bind(R.id.listView)
    ListView listView;

    SystemMsgAdapter adapter;
    List<GetMsgBean.DataBean> list;

    UserPresenter presenter;
    MyMsgActivity myMsgActivity;
    @Bind(R.id.my_msg_system_refresh)
    SwipeRefreshLayout myMsgSystemRefresh;
    SwipeRefreshHelper swipeRefreshHelper;
    String userid;
    int page = 1;
    boolean isLastPage, isLoading;
    LoadingDialog dialog;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_system_msg, container, false);
        ButterKnife.bind(this, view);
        EventBus.getDefault().register(this);
        myMsgActivity = (MyMsgActivity) getActivity();
        presenter = new UserPresenterImp();
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        loadData();

    }

    @Override
    public void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    private void loadData() {
        UserBean.DataEntity user = new UserBean.DataEntity();
        user.readInfoFromLocal(getActivity());

        userid = user.getUserid();
        isLoading = true;
        myMsgSystemRefresh.setRefreshing(true);

        presenter.getMsg(userid, "1", "9", String.valueOf(page));

        list = new ArrayList<>();
        adapter = new SystemMsgAdapter(getActivity(), list);
        listView.setAdapter(adapter);
        swipeRefreshHelper = new SwipeRefreshHelper(myMsgSystemRefresh, listView, new SwipeRefreshHelper.OnSwipeRefreshListener() {
            @Override
            public void onRefresh() {
                if (!isLoading) {
                    list.clear();
                    isLastPage = false;
                    adapter.notifyDataSetChanged();
                    page = 1;
                    presenter.getMsg(userid, "1", "9", String.valueOf(page));
                }
            }

            @Override
            public void onLoad() {
                if (!isLastPage) {
                    page++;
                    presenter.getMsg(userid, "1", "9", String.valueOf(page));
                } else {
                    DeviceUtil.MyToast(getActivity(), "已经是最后一页");
                }
            }
        });

    }

    public void onEventMainThread(GetMsgBean bean) {

        isLoading = false;

        if (myMsgSystemRefresh.isRefreshing()) {
            myMsgSystemRefresh.setRefreshing(false);
        }

        if (bean.getReturn_code() == 0) {
            list.addAll(bean.getData());
            adapter.notifyDataSetChanged();
            myMsgActivity.setSystemUnread(bean.getUnread());
        } else if (bean.getReturn_code() == -203){
            isLastPage = true;
            DeviceUtil.MyToast(getActivity(), "已经是最后一页");
        } else {
            DeviceUtil.MyToast(getActivity(), bean.getMsg());
        }
    }

    public void onEventMainThread(ErrorBean bean) {
        if (myMsgSystemRefresh.isRefreshing()) {
            myMsgSystemRefresh.setRefreshing(false);
        }
        DeviceUtil.MyToast(getActivity(), bean.getErrorMsg());
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
